/**
 * @Author: LiMeng
 * @Date: 2017/4/6
 */
$(function () {
    var EMPTY_OPTION = {
        code: '',
        name: '请选择'
    };

    //初始化国家下拉框
    $('#country').combobox({
        url: ROOT_PATH + "/permission/district/country.shtml",
        valueField: 'code',
        textField: 'name',
        editable: false,
        loadFilter: function (data) {
            data.unshift(EMPTY_OPTION);
            return data;
        },
        onClick: function (record) {
            if (record.name.toString() === '中国') {
                $('.district-wrap').show();
            } else {
                $('.district-wrap').hide();
            }
        }
    });

    //初始化省份下拉框
    $('#province').combobox({
        url: ROOT_PATH + "/permission/district/province.shtml",
        valueField: 'code',
        textField: 'name',
        editable: false,
        loadFilter: function (data) {
            data.unshift(EMPTY_OPTION);
            return data;
        },
        onClick: function (record) {
            if (record.code === '') {
                $('#city').combobox('clear').combobox('loadData', [EMPTY_OPTION]);
                $('#county').combobox('clear').combobox('loadData', [EMPTY_OPTION]);
                return true;
            }
            $.ajax({
                method: 'GET',
                dataType: 'JSON',
                url: ROOT_PATH + "/permission/district/city.shtml?parentCode=" + record.code,
                success: function (data) {
                    data.unshift(EMPTY_OPTION);
                    $('#city').combobox('clear').combobox('loadData', data);
                    $('#county').combobox('clear').combobox('loadData', [EMPTY_OPTION]);
                }
            });
        }
    });

    //初始化城市下拉框
    $('#city').combobox({
        valueField: 'code',
        textField: 'name',
        editable: false,
        data: [EMPTY_OPTION],
        onClick: function (record) {
            if (record.code === '') {
                $('#county').combobox('clear').combobox('loadData', [EMPTY_OPTION]);
                return true;
            }
            $.ajax({
                method: 'GET',
                dataType: 'JSON',
                url: ROOT_PATH + "/permission/district/county.shtml?parentCode=" + record.code,
                success: function (data) {
                    data.unshift(EMPTY_OPTION);
                    $('#county').combobox('clear').combobox('loadData', data);
                }
            });
        }
    });

    //初始化区县下拉框
    $('#county').combobox({
        valueField: 'code',
        textField: 'name',
        editable: false,
        data: [EMPTY_OPTION]
    });

    $('#btn_save').on('click', function () {
        $.messager.progress();	// 显示进度条

        $('#form1').form('submit', {
            url: ROOT_PATH + '/contractor/contractorInfo/add.shtml',
            onSubmit: function (param) {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
                }
                return isValid;	// 返回false终止表单提交
            },
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $.getIframeWindow().$('#dg1').datagrid('load');
                        var id = result.data.id;
                        window.location.href = ROOT_PATH + '/contractor/contractorInfo/edit.shtml?id=' + id
                            + '&resourceId=' + $.getQueryParam('resourceId')
                            + '&state=edit';
                    });
                } else {
                    $.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message, {time: 1000, icon: 2});
                }
            }
        });
    });
});